package yf.data;

import org.testng.ITestNGMethod;
import org.testng.Reporter;
import org.testng.internal.ConstructorOrMethod;
import yf.annotations.DataSource;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.DataProvider;
import yf.bean.TestCase;
import yf.common.setUpTearDown;
import yf.utils.GetTestCase;
import com.google.gson.Gson;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class DataProviders {
    private static final Logger log = LoggerFactory.getLogger(DataProviders.class);
    private static  Object[][] objects;
    @DataProvider(name="getData")
    public static Object[][] getData(Method method ) throws IOException {
        DataSource dataSource = null;
        Map<String,String> caseMessMap=new HashMap<>();
        if(method.isAnnotationPresent(DataSource.class )){
            dataSource=method.getAnnotation(DataSource.class);
            TestCase testCase=GetTestCase.getTestCase(dataSource.filePath(), dataSource.sheetName());
            objects = new Object[testCase.getCases().size()][2];

            for(int i=0;i<testCase.getCases().size();i++){
//                objects[i][0]=testCase.getCases().get(i).getTestCaseNo();

                String parmas=testCase.getCases().get(i).getParameter();
//                JSONObject jsonObject = JSONObject.parseObject(parmas);
//                Map<String, Object> map = (Map) jsonObject;
//                new Gson().toJson(map);
                objects[i][0]=parmas;
//                objects[i][2]=testCase.getMethod();
//                objects[i][3]=testCase.getPath();

                caseMessMap.put("caseNo",testCase.getCases().get(i).getTestCaseNo());
                caseMessMap.put("requestType",testCase.getRequestType());
                caseMessMap.put("path", testCase.getPath());
                caseMessMap.put("result", testCase.getCases().get(i).getResult());
                caseMessMap.put("sqlResult", testCase.getCases().get(i).getSqlResult());
                caseMessMap.put("testPoint", testCase.getCases().get(i).getTestPoint());
                objects[i][1]=new Gson().toJson(caseMessMap);

            }
        }else{
            log.error("未指定@DataSource注解却初始化了dataProvider");
        }
//        try (InputStream in = DataProviders.class.getResourceAsStream(dataSource.filePath());){
//
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        caseMessMap.clear();
        return objects;
    }


}


